package com.dsg.service.account.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.dsg.common.util.SingletonLoginUtils;
import com.dsg.common.util.UUIDGenerator;
import com.dsg.common.util.toolbox.CollectionUtil;
import com.dsg.common.util.toolbox.StringUtil;
import com.dsg.dao.account.DsgAccountCompareMapper;
import com.dsg.entity.account.DsgAccountCompare;
import com.dsg.service.DsgServiceImpl;
import com.dsg.service.account.IDsgAccountCompareService;
import com.dsg.service.account.IDsgAccountService;
import com.dsg.util.RRException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;
import java.util.List;

/**
 * <p>
 * 天鑽公司与银行账比较 服务实现类
 * </p>
 *
 * @author system
 * @since 2020-11-18
 */
@Service
public class DsgAccountCompareServiceImpl extends DsgServiceImpl<DsgAccountCompareMapper, DsgAccountCompare> implements IDsgAccountCompareService {

    @Autowired
    private IDsgAccountService dsgAccountService;

    @Override
    public DsgAccountCompare getAddDsgAccountCompare(String accountNo, String accountName) {
        if (StringUtil.isEmpty(accountNo)) {
            throw new RRException("收款銀行賬號不能為空!");
        }
        DsgAccountCompare dsgAccountCompare = this.getNewDsgAccountCompare(accountNo, SingletonLoginUtils.getSystemEntCode());
        if (dsgAccountCompare != null) {
            return dsgAccountCompare;
        } else {
            dsgAccountCompare = new DsgAccountCompare();
            dsgAccountCompare.setOid(UUIDGenerator.getUUID());
            dsgAccountCompare.setCheckStatus(DsgAccountCompare.CHECK_STATUS_1);
            dsgAccountCompare.setAccountName(accountName);
            dsgAccountCompare.setAccountNo(accountNo);
            dsgAccountCompare.setBankStaSum(BigDecimal.ZERO);
            dsgAccountCompare.setBankUseSum(BigDecimal.ZERO);
            dsgAccountCompare.setSysStaSum(BigDecimal.ZERO);
            dsgAccountCompare.setSysUseSum(BigDecimal.ZERO);
            dsgAccountCompare.setIsHis(DsgAccountCompare.IS_HIS_1);
            baseMapper.insert(dsgAccountCompare);
            return baseMapper.selectById(dsgAccountCompare.getOid());
        }
    }

    @Override
    public DsgAccountCompare getSumDsgAccountCompare(String accountNo, String entCode) {
        return baseMapper.getSumDsgAccountCompare(accountNo, entCode);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public synchronized DsgAccountCompare checkBank(DsgAccountCompare dsgAccountCompare) {
        if (dsgAccountCompare == null) {
            throw new RRException("check bank數據不能為空!");
        }
        if (StringUtil.isEmpty(dsgAccountCompare.getAccountNo())) {
            throw new RRException("程序參數錯誤:銀行賬號為空!");
        }
        String entCode = SingletonLoginUtils.getSystemEntCode();
        DsgAccountCompare sysAccount = baseMapper.getSumDsgAccountCompare(dsgAccountCompare.getAccountNo(), entCode);
        if (sysAccount != null) {
            dsgAccountCompare.setSysStaSum(sysAccount.getSysStaSum());
            dsgAccountCompare.setSysUseSum(sysAccount.getSysUseSum());
            dsgAccountCompare.setIsHis(DsgAccountCompare.IS_HIS_1);
            if (sysAccount.getSysStaSum().compareTo(dsgAccountCompare.getBankStaSum()) == 0 ||
                    sysAccount.getSysUseSum().compareTo(dsgAccountCompare.getBankUseSum()) == 0) {
                dsgAccountCompare.setCheckStatus(DsgAccountCompare.CHECK_STATUS_1);
                dsgAccountCompare.setIsPassed("1");
            } else {
                dsgAccountCompare.setIsPassed("0");
                dsgAccountCompare.setCheckStatus(DsgAccountCompare.CHECK_STATUS_0);
            }
            sysAccount = this.getNewDsgAccountCompare(dsgAccountCompare.getAccountNo(), entCode);
            if (sysAccount != null) {
                sysAccount.setIsHis(DsgAccountCompare.IS_HIS_2);
                baseMapper.updateById(sysAccount);
            }
            dsgAccountCompare.setOid(UUIDGenerator.getUUID());
            baseMapper.insert(dsgAccountCompare);
        } else {
            throw new RRException("未查詢天鑽公司總帳數據!");
        }
        return dsgAccountCompare;
    }

    @Override
    public DsgAccountCompare getNewDsgAccountCompare(String accountNo, String entCode) {
        QueryWrapper<DsgAccountCompare> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("ACCOUNT_NO", accountNo);
        queryWrapper.eq("ENT_CODE", SingletonLoginUtils.getSystemEntCode());
        queryWrapper.eq("IS_HIS", DsgAccountCompare.IS_HIS_1);
        List<DsgAccountCompare> compareList = baseMapper.selectList(queryWrapper);
        if (CollectionUtil.isNotEmpty(compareList)) {
            return compareList.get(0);
        }
        return null;
    }
}
